package net.f3322.lingwl13.mapper;

import com.github.pagehelper.Page;
import net.f3322.lingwl13.domain.Book;
import org.apache.ibatis.annotations.*;

public interface BookMapper {
    @Select("SELECT * FROM book WHERE book_status != '3' order by book_uploadtime DESC")
    @Results(id = "bookMap",value = {
//id字段默认为false，表示不是主键
//column表示数据库表字段，property表示实体类属性名。
            @Result(id = true ,column = "book_id",property = "id"),
            @Result(column = "book_name",property = "name"),
            @Result(column = "book_isbn",property = "isbn"),
            @Result(column = "book_press",property = "press"),
            @Result(column = "book_author",property = "author"),
            @Result(column = "book_pagination",property = "pagination"),
            @Result(column = "book_price",property = "price"),
            @Result(column = "book_uploadtime",property = "uploadTime"),
            @Result(column = "book_status",property = "status"),
            @Result(column = "book_borrower",property = "borrower"),
            @Result(column = "book_borrowtime",property = "borrowTime"),
            @Result(column = "book_returntime",property = "returnTime")
    })
    Page<Book> selectNewBooks();

    @Select("SELECT * FROM book WHERE book_id=#{id}")
    @ResultMap("bookMap")
    Book findById(String id);

    Integer editBook(Book book);

    @Select({
            "<script>" +
                    "SELECT * FROM book WHERE book_status != 3" +
                    "<if test=\"name!=null\">" +
                    "AND book_name LIKE CONCAT('%',#{name},'%')</if>" +
                    "<if test=\"press !=null\">" +
                    "AND book_press LIKE CONCAT('%',#{press},'%')</if>" +
                    "<if test=\"author!=null\">" +
                    "AND book_author LIKE CONCAT('%',#{author},'%')</if>" +
                    "ORDER BY book_status" +
                    "</script>"
    })
    @ResultMap("bookMap")
    Page<Book> searchBooks(Book book);

    Integer addBook(Book book);

    @Select({
            "<script>" +
                    "SELECT * FROM book " +
                    "WHERE book_borrower=#{borrower} " +
                    "AND book_status='1' " +
                    "<if test=\"name!=null\"> AND book_name LIKE CONCAT('%',#{name},'%')</if>" +
                    "<if test=\"press!=null\"> AND book_press LIKE CONCAT('%',#{press},'%')</if>" +
                    "<if test=\"author!=null\"> AND book_author LIKE CONCAT('%',#{author},'%')</if> " +
                    "or book_status='2'" +
                    "<if test=\"name!=null\"> AND book_name LIKE CONCAT('%',#{name},'%')</if>" +
                    "<if test=\"press!=null\"> AND book_press LIKE CONCAT('%',#{press},'%')</if>" +
                    "<if test=\"author!=null\"> AND book_author LIKE CONCAT('%',#{author},'%')</if> " +
                    "ORDER BY book_borrowtime" +
                    "</script>"
    })
    @ResultMap("bookMap")
    Page<Book> selectBorrowed(Book book);

    @Select({
            "<script>" +
                    "SELECT * FROM book " +
                    "WHERE book_borrower=#{borrower} " +
                    "AND book_status='1' " +
                    "<if test=\"name!=null\"> AND book_name LIKE CONCAT('%',#{name},'%')</if>" +
                    "<if test=\"press!=null\"> AND book_press LIKE CONCAT('%',#{press},'%')</if>" +
                    "<if test=\"author!=null\"> AND book_author LIKE CONCAT('%',#{author},'%')</if> " +
                    "ORDER BY book_borrowtime" +
                    "</script>"
    })
    @ResultMap("bookMap")
    Page<Book> selectMyBorrowed(Book book);
}
